home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
7_4.lha
/
7_4
/
7_4a.c
next >
Wrap
Text File
|
1993-08-08
|
2KB
|
65 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ exercise 7.4
/ connect two shapes
include <shape.h>
/ return the square of the
/ distance between two points
nline int dist(point a, point b)
int deltax = a.x - b.x;
int deltay = a.y - b.y;
return deltax * deltax + deltay * deltay;
/ find the closest contact-points between
/ two shapes and draw a line between them
oid connectshapes(const shape *a, const shape *b)
// save each of the 8 points relevant to each shape
point apts[8], bpts[8];
point *ptptr = apts;
const shape *shptr = a;
for (int i = 0; i < 2; i++, shptr = b, ptptr = bpts)
{
*ptptr++ = shptr->north();
*ptptr++ = shptr->neast();
*ptptr++ = shptr->east();
*ptptr++ = shptr->seast();
*ptptr++ = shptr->south();
*ptptr++ = shptr->swest();
*ptptr++ = shptr->west();
*ptptr++ = shptr->nwest();
}
// save one set of points and their distance
point pta = apts[0], ptb = bpts[0];
int mindist = dist(pta, ptb);
// find closest point on shape b to pta
int ndist;
for (i = 1; i < 8; i++)
if ((ndist = dist(pta, bpts[i])) < mindist)
{
ptb = bpts[i];
mindist = ndist;
}
// look for a set of points even closer
for (i = 1; i < 8; i++)
for (int j = 0; j < 8; j++)
if ((ndist = dist(apts[i], bpts[j])) < mindist)
{
pta = apts[i];
ptb = bpts[j];
mindist = ndist;
}
// draw a line between the two points
put_line(pta, ptb);